package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.system.bean.LogInfo;
import com.tjtt.tdm.system.service.LogService;
/**
 * 系统设定管理
 * 
 * @author 宿馨月
 * 
 */
public class LogAction extends BaseAction{
	private LogService logService;
	private LogInfo logInfo;
	private Long logId;
	/**
     * 查询日志管理数据显示到jqrid
     * @throws Exception void
     * @author 宿馨月
     * @date 2012-9-03 上午10:30:58
     */
	public void searchLogListExec() throws Exception{
		try {
			Map<String, String> filterMap = getPagerMap();
		    //取出页面中的查询条件

			String logModuleName = getRequest().getParameter("logModuleName");
			String logOrgName = getRequest().getParameter("logOrgName");
			String logUserName =getRequest().getParameter("logUserName");
			String logLoginIp = getRequest().getParameter("logLoginIp");
			String logType = getRequest().getParameter("logType");
			String logStarTime = getRequest().getParameter("logStarTime");
			String logEndTime = getRequest().getParameter("logEndTime");
			//解决乱码问题
			if(logModuleName != null && !"".equals(logModuleName)){
				logModuleName = StringUtil.encodingUrl(logModuleName);
			}
			if(logOrgName != null && !"".equals(logOrgName)){
				logOrgName = StringUtil.encodingUrl(logOrgName);
			}
			if(logUserName != null && !"".equals(logUserName)){
				logUserName = StringUtil.encodingUrl(logUserName);
			}
			if(logType != null && !"".equals(logType)){
				logType = StringUtil.encodingUrl(logType);
			}
			//将查询条件保存到Map里
			filterMap.put("logModuleName", logModuleName);
			filterMap.put("logOrgName", logOrgName);
			filterMap.put("logUserName", logUserName);
			filterMap.put("logLoginIp", logLoginIp);
			filterMap.put("logType", logType);
			filterMap.put("logStarTime", logStarTime);
			filterMap.put("logEndTime", logEndTime);
			filterMap.put("userid", getUserId());
			
			String[] viewArray = { "LOG_ID", 
					"LOG_ORG_NAME","LOG_USER_NAME", "LOG_TYPE","LOG_MODULE_NAME","LOG_LOGIN_EVENT", "LOG_LOGIN_IP", "LOG_CREATTIME"};
			// 查询所有的数据
			PageResult result = logService.getLogList(filterMap);
			String returnStr = getColumnJson(result, viewArray);

			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void clearLog() throws Exception{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		try {
			logService.deleteLogAll();
			out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"success\"}");
		}
	}
    /**
     * 根据id查看日志信息
     * @return String
     * @author 宿馨月
     * @date 2012-9-03 下午16:15:33
     */
    public String getLogByIdExec(){
    	logInfo = logService.findLogById(logId);
    	return SUCCESS;
    }
	
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	public LogInfo getLogInfo() {
		return logInfo;
	}
	public void setLogInfo(LogInfo logInfo) {
		this.logInfo = logInfo;
	}
	public Long getLogId() {
		return logId;
	}
	public void setLogId(Long logId) {
		this.logId = logId;
	}
	
}
